@file:Suppress("unused")

package xyz.myachin.downloader

import android.app.Application
import android.content.pm.PackageManager
import xyz.myachin.downloader.dm.Downloader
import xyz.myachin.downloader.settings.Settings
import xyz.myachin.downloader.workmanagers.RemoveDownloadsWorker

class App : Application() {
    override fun onCreate() {
        super.onCreate()
        Settings.setup(applicationContext)
        Downloader.setup(applicationContext)
        RemoveDownloadsWorker.init(applicationContext)
        Thread {
            showSettingsIfNeed()
        }.start()

        Thread {
            disableProxyFeatureIfItNeed()
        }.start()
    }

    private fun disableProxyFeatureIfItNeed() {
        if (Settings.isProxyEnabled && Settings.isProxyMustDisabled) {
            Settings.setProxyFeatureState(
                applicationContext,
                PackageManager.COMPONENT_ENABLED_STATE_DISABLED
            )
            Settings.isProxyEnabled = false
        }
    }

    private fun showSettingsIfNeed() {
        if (Settings.hideLauncherIcon) {
            if (Settings.versionSettings != getString(R.string.settings_version)) {
                Settings.hideLauncherIcon = false
                Settings.versionSettings = getString(R.string.settings_version)
                Settings.setLauncherIconState(
                    applicationContext,
                    PackageManager.COMPONENT_ENABLED_STATE_DEFAULT
                )
            }
        }
    }
}